<?php
	/**
	 *	Copyright (c) TeamFlamingo And gray 2004-2009
	 *	www.me-live.com.cn / www.i-gray.cn
	 *
	 *	[ WARNING ]
	 *		This is NOT a freeware!
	 *		You MUST get author's authorization before using it!
	 *	[ WARNING ]
	 *
	 *	Name : paging.h.php / Development Code Evolve
	 *	Created / Modify : 2010-2-17 / --
	 */

	if( !defined( 'IN_TEAME' ) )
	{
		exit( 'Access Denied' );
	}

	/**
	 *	分页主函数
	 *	@parma int $postsTotal	文章总数
	 *	-- @param static $maxPostsNumber	允许显示的文章数
	 *	@param array $dblimit	由slqLimit生成的数组
	 */
	function	parsePaging ( $postsTotal, $dblimit/*, $maxPostsNumber*/ )
	{
		$loop	=	$MDKPAGE	=	$list	=	array();

		parse_str( $_SERVER['QUERY_STRING'], $MDKPAGE );
		unset( $MDKPAGE['p'] );

		//	[CH]	重新组建URL
		$sourceURL	=	reBuildURL( $MDKPAGE );
		$postsTotal	=	getMeInt( $postsTotal );
		$maxPostsNumber	=	$dblimit[3];
		$page			=	$dblimit[2];

		//	[CH]	控制向前 / 向后翻页
		$prev	=	$page - 1;
		$next	=	$page + 1;

		//	[CH]	获取可分页数
		$list['total']	=	ceil( $postsTotal / $maxPostsNumber );
		$list['current']	=	$page;

		$list['home']	=	$page != 1 ? "&nbsp; <a href=\"{$sourceURL}p=1\">首页</a>&nbsp;" : '';
		$list['prev']	=	$page != 1 ? "&nbsp;<a href=\"{$sourceURL}p={$prev}\">上一页</a>&nbsp;" : '';

		//	[CH]	获取循环的起始页码
		$loop['start']	=	getMeInt( ( $page - 1 ) / ME_MAXPAGENUMBER ) * ME_MAXPAGENUMBER;
		//	[CH]	获取循环的结束页码
		$loop['end']	=	( ( $list['total'] - $loop['start'] ) > ME_MAXPAGENUMBER ) ? ME_MAXPAGENUMBER + $loop['start'] : $list['total'];

		//	[CH]	获取下一组页码
		$prev_group		=	$loop['start'] - ME_MAXPAGENUMBER + 1;
		$next_group		=	$loop['start'] + ME_MAXPAGENUMBER + 1;

		$list['prev_group']	=	( $prev_group >= 0 ) ? "&nbsp;<a href=\"{$sourceURL}p={$prev_group}\">上十页</a>" : '';

		//	[CH]	页码列表
		for( $i = $loop['start'] + 1; $i <= $loop['end']; $i++ )
		{
			$list['list'][]	=	( $page == $i ) ? "<b>{$i}</b>" : "<a href=\"{$sourceURL}p={$i}\">{$i}</a>";
			//if( $page == $i )
			//$list['current'] = $page == $i ? $i : '';
		}

		$list['next']	=	$page < $list['total'] ? "&nbsp;<a href=\"{$sourceURL}p={$next}\">下一页</a>&nbsp;" : '';
		$list['next_group']	=	$next_group <= $list['total'] ? "&nbsp;<a href=\"{$sourceURL}p={$next_group}\">下十页</a>&nbsp;" : '';
		$list['last']	=	$page < $list['total'] ? "&nbsp;<a href=\"{$sourceURL}p={$list['total']}\">尾页</a>" : '';

		return	$list;
		/*return	$postsTotal;*/
	}

	/**
	 *	返回sql语句的Limit条件
	 *	@param	int	$maxPostsNumber	允许显示的文章数
	 */
	function	sqlLimit( $maxPostsNumber )
	{
		global	$MDK;
		$page	=	( isset( $MDK['p'] ) && !empty( $MDK['p'] ) && getMeInt( $MDK['p'] ) >= 1 ) ? getMeInt( $MDK['p'] ) : '1';
		$sql_limit_start	=	( $page - 1 ) * $maxPostsNumber;
		$sql_limit_end		=	$maxPostsNumber;
		return	array( getMeInt( $sql_limit_start ), getMeInt( $sql_limit_end ), $page, $maxPostsNumber );
	}

	//	[CH]	重新组建URL
	function	reBuildURL ( $arr )
	{
		$string	=	'';
		//	[CH]	定义可用的GET数据
		$action	=	array( 'do', 'f', 't' );

		foreach( $arr as $key => $value )
			if( in_array( $key, $action ) )
				$string	.= $key . '=' . $value . '&';

		return	'?' . $string;
	}
?>